package com.demo.util;

import com.demo.entity.User;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;


public class CheckToken {

    public TokenVo checkToken(HttpServletRequest request) {
        //获取用户的头部信息的token
        String token = request.getHeader("authorization");
        if (token != null) {
            //获取解析后的token令牌
            TokenVo tokenVo = TokenUtils.verifyToken(token);
            if (tokenVo != null) {//解析token是否过期
                User user = new User();
                //校验token是否合法 并且是否过期
                if (tokenVo != null && user != null
                        && Objects.equals(user.getUsername(), tokenVo.getUsername())
                        && Objects.equals(user.getPassword(), tokenVo.getPassword())) {
                    return tokenVo;
                } else {//非法token
                    return null;
                }
            } else {
                return null;
            }
        } else {//没有token
            return null;
        }
    }
}
